
/**
 * treetable组件
 */
( function($) {
	var MyTreeTable = $.MyTreeTable = function(el, options) {
		this.el = el;
		this.options = $.extend({},this.options, options);
		// 创建tabs
		this.createtable();
	};

	// 扩展原型
	$.extend(MyTreeTable.prototype, {
		layer :null,
		options : {
		    viceflag:'',//是否存在副维度1:有副维度;0：没有副维度
		    iscontra:'',//是否收缩扩展 1：有扩展 ;0:没有扩展
		    vicedim:[{
		     maxfloor:'',
		     dimcode:'',
		     ordercode:'',
		     table:'',
		     isqd:'',
		     dimname:''
		    }],        //存放副维度对象 
		    viceaction:'',//副维度访问的类
		    iscross:'',//是否跨层
		    isrightkey:'',//是否右键
		    isothertable:'',
		    maindim:{  //存放主维度对象
		     maincode:'',   //主维度代码
		     maxfloor:'',   //主维度最大层级
		     ordercode:'',  //主维度排序代码
		     table:'',     //主维度表名
		     isqd:''       //主维度是否是渠道架构
	        },
		    domid:'',//添加table的div节点id
		    head:[],  //表头数组
		    contions:[], //存放结果集map中的key值 
		    
		    dataflag:'1', //数据标识   1：数组;2:map
		    conhasqd:'',  //条件中是否有渠道
		    //样式变量
		    hideclass:'expand',  //隐藏样式
		    contraclass:'switch',
		    firstthclass:'col1', //第一列的样式
		    tablecalss:'table-normal table-line',
		    expandtitle:'', //表头展现的title
		    
		    //不用改变
		    dillclass:'floor',
		    dillid:'drillid_',
		    dillmargin:'1',
		    dillfloor:'',  //下钻时的层级，实际是用户归属的层级
		    dillotherfloor:'1'
		    	
		    
		},
	    action:function(dom,opt,mytable){ //点击事件
			var $dom=$(dom);
			var orgfloor=$dom.parent().parent().attr("orgfloor");
			var otherfloor=$dom.parent().parent().attr("otherflag");
			var pmargin=$dom.parent().parent().attr("margin");
			var pclass=$dom.parent().parent().attr("class1");
			var pid=$dom.attr("id");
			 var isexit=$("[class1="+pclass+"_0]");
			 var display=$("[class1="+pclass+"_0]").css("display")=="none";
			 if(isexit.length>0){ //判断下一级是否存在
				 if(!display){ //判断下一级是否显示
					 $dom.children().removeClass("sub");
				     $dom.children().addClass("sub_on");
					 $("[class1^='"+pclass+"_']").hide();
				 }else{
					 $dom.children().removeClass("sub_on");
					 $dom.children().addClass("sub");
					 $("[class1^='"+pclass+"_']").hide();
					 $("[class1^='"+pclass+"_'][flag='"+pid+"']").show();
				 }
			 }else{
				pmargin=parseInt(pmargin)+15;
				if(opt.maindim.isqd==1){
					orgfloor=parseInt(orgfloor)+1;
				 }else{
				    otherfloor=parseInt(otherfloor)+1; 
				 }
				var obj={
						pid:$dom.attr("id"),
						pclass:pclass,
						pmargin:pmargin,
						floor:orgfloor,
						otherfloor:otherfloor
					};
				 var datas=mytable.call(opt.getdowndata,obj,opt.maindim);
				 var tr=mytable.addtr(datas,obj);
				 $dom.parent().parent().after(tr);
				 $("[id^="+obj.pid+"_]").click(function(){
					 mytable.call(mytable.action,this,opt,mytable);
				 });
				 this.addtrmouse();
			 }
			// $("tr:visible:even").addClass("spec");
		},
	    init:function(){
			this.call(this.options.init, event);
		},
		createtable:function(){  //创建table
			var mytable=this;
			var opt=mytable.options;
			var obj={
				pid:opt.dillid,
				pclass:opt.dillclass,
				pmargin:opt.dillmargin,
				floor:opt.dillfloor,
				otherfloor:opt.dillotherfloor
			};
			var table="<table border='0' cellspacing='0' cellpadding='0' class='"+opt.tablecalss+"'>";
			 table+="<thead>";
			 table+=mytable.createhead();
			 table+="</thead><tbody>";
			 var datas=mytable.call(mytable.options.getinitdata,opt.maindim);
			 table+=mytable.createtbody(datas, obj);
			 table+="</tbody></table>";	
			 $("#"+opt.domid).append(table);
			  mytable.addtrmouse();
		     //  $("table tr:visible :even","#"+opt.domid).addClass("spec");
		     $("[id^="+opt.dillid+"]").click(function(){
		    	 mytable.call(mytable.action,this,opt,mytable);
		    	
		     });
		},
		addtr:function(data,obj){
			var tr="";
			var opt=this.options;
			var ththis=this;
			var floor;
			if(opt.maindim.isqd=="1"){
				floor=obj.floor;
			}else{
				floor=obj.otherfloor;
			}
			$.each(data,function(i,con){
				if(opt.dataflag=="1"){ //data数据集为数组的时候
					tr+= "<tr class1='"+obj.pclass+"_"+i+"' margin='"+obj.pmargin+"' flag='"+obj.pid+"' orgfloor='"+obj.floor+"' otherflag='"+obj.otherfloor+"'>";
					tr+=ththis.createtdArraycon(con,i,floor,2,obj.pid,obj.pmargin);
					tr+=ththis.createdvicedim(floor, con[0],con[con.length-1] , obj.otherfloor, obj.floor);
				}else{  //data数据集为Map的时候
					tr+= "<tr class1='"+obj.pclass+"_"+i+"' margin='"+obj.pmargin+"' flag='"+obj.pid+"' orgfloor='"+obj.floor+"'  otherflag='"+obj.otherfloor+"'>";
					tr+=ththis.createthMapcon(data, i,floor, 2, obj.pid,obj.pmargin);
					tr+=ththis.createdvicedim(floor, opt.contions[0],con["groupid"] , obj.otherfloor, obj.floor);
				}
				tr+="<tr>";
			});
			return tr;
		},
		
		/**
		 * 处理数据为Map时初始化和下钻时th的内容
		 * data：数据
		 * flag：tr的行编号
		 * floor：当前下钻的层次
		 * type：标识1：初始化;2：下钻
		 * pid：下钻时用到的父id
		 */
		createthMapcon:function(data,flag,floor,type,pid,pmargin){
			var opt=this.options;
			var th='',text='';
			if(type=="1"){
				parentid=opt.dillid+flag;
			}else{
				parentid=pid+"_"+flag;
			}
			var groupid="groupid";
			$.each(opt.contions,function(i){
				text=data[this.toLowerCase()]
				if(i=="0"){  //处理数据的第一列
					if(floor<opt.maindim.maxfloor){
						 th+="<td class='"+opt.firstthclass+"' style='text-align: left' ><span id='"+parentid+"' style='margin-left:"+pmargin+"px' stext='"+text+"' drillvalue='"+data[groupid]+"'><a href='javascript:void(0)' class='sub_on'>"+text+"</a></span></td>";
					}else{
						th+="<td class='"+opt.firstthclass+"' style='text-align: left' ><span  style='margin-left:"+pmargin+"px' stext='"+text+"' drillvalue='"+data[groupid]+"'><a href='javascript:void(0)' class='root'>"+text+"</a></span></td>";
					}
				}else{
					if(type=="1"){
						if(flag=="0"){
							var len=con.length;
							var space='';
							if(len<14){
								for(var b=0;b<14;b++){space+="&nbsp;";}
							}
							th+="<td>"+space+text+"</td>";
						}else{
							th+="<td>"+text+"</td>";
						}
					}else{
						th+="<td>"+text+"</td>";
					}
				}
			});
			return th;
		},
		/**
		 * 处理数据为数组时初始化和下钻时th的内容
		 * data：数据
		 * flag：tr的行编号
		 * floor：当前下钻的层次
		 * type：标识1：初始化;2：下钻
		 * pid：下钻时用到的父id
		 * orgfloor:用户归属层
		 * otherfloor:其他层级
		 */
		createtdArraycon:function(data,flag,floor,type,pid,pmargin){   // 
			var opt=this.options;
			var parentid='';
			var th='';
			if(type=="1"){ //判断度维度是初始化还是下钻
				parentid=opt.dillid+flag;
			}else{
				parentid=pid+"_"+flag;
			}
			var groupid=data.length-1;  //取主维度的值
			$.each(data,function(i,con){
			   if(i<groupid){  
					if(i=="0"){  //处理第一列显示
						if(floor<opt.maindim.maxfloor){  //当列前下钻层小于该维度最大层
						   th+="<td class='"+opt.firstthclass+"' style='text-align: left' ><span id='"+parentid+"' stext='"+con+"' style='margin-left:"+pmargin+"px' drillvalue='"+data[groupid]+"'><a href='javascript:void(0)' class='sub_on'>"+con+"</a></span></td>";
						}else{
						   th+="<td class='"+opt.firstthclass+"' style='text-align: left' ><span  stext='"+con+"' style='margin-left:"+pmargin+"px' drillvalue='"+data[groupid]+"'><a href='javascript:void(0)' class='root'>"+con+"</a></span></td>";	
						}
					}else{
						if(type=="1"){
							if(flag=="0"){
								var len=con.length;
								var space='';
								if(len<14){
									for(var b=0;b<14;b++){space+="&nbsp;";}
								}
								th+="<td>"+space+con+"</td>";
							}else{
								th+="<td>"+con+"</td>";
							}
						}else{
							th+="<td>"+con+"</td>";
						}
					}
				}
			   
			});
			return th;
		},
		/**
		 * 创建表头
		 */
		createhead:function(even){   //处理表头
			var tr="<tr>";
			var opt=this.options;
			if(opt.iscontra=="1"){
				tr+=this.createContrTh();
			}else{
				tr+=this.createth();
			}
			tr+="</tr>";
			return tr;
		}, 
		/**
		 * 处理tbody中的数据
		 * data：为数据集
		 * obj为对象，包括pid:父节点id,pclass:父节点tr的样式,pmargin:父节点的缩进,floor:用户组织层级,otherfloor：其他维度的层级
		 * 
		 */
		createtbody:function(data,obj){  //
			var tr="";
			var thisth=this;
			var opt=this.options;
			var floor;
			if(opt.maindim.isqd=="1"){
				floor=obj.floor;
			}else{
				floor=obj.otherfloor;
			}
			$.each(data,function(i,con){
				if(opt.dataflag=="1"){ //data数据集为数组的时候
					tr+= "<tr class1='"+obj.pclass+"_"+i+"' margin='"+obj.pmargin+"' flag='"+obj.pid+"' orgfloor='"+obj.floor+"' otherflag='"+obj.otherfloor+"'>";
					tr+=thisth.createtdArraycon(con,i,floor,1,obj.pid,obj.pmargin);
					tr+=thisth.createdvicedim(floor,con[0],con[con.length-1] , obj.otherfloor, obj.floor);
					
				}else{  //data数据集为Map的时候
					tr+= "<tr class1='"+obj.pclass+"_"+i+"' margin='"+obj.pmargin+"' flag='"+obj.pid+"' orgfloor='"+obj.floor+"' otherflag='"+obj.otherfloor+"'>";
					tr+=thisth.createthMapcon(data, i, floor, 1, obj.pid,obj.pmargin);
					tr+=thisth.createdvicedim(floor,con[opt.contions[0]],con["groupid"] , obj.otherfloor, obj.floor);
				}
				tr+="<tr>";
			});
		 return tr;
		},
		/**
		 * //创建不带扩展表头th
		 */
		createth:function(){   
			var th='';
			var opt=this.options;
			$.each(opt.head,function(i,head){
			  if(i==0){
				th+="<th calss="+opt.firstthclass+">"+head+"</th>";
			   }else{
				th+="<th >"+head+"</th>";	
			   }
			});
			if(opt.viceflag=="1"){
				th+="<th>查看明细</th>";
			}
			return th;
		},
		/**
		 * //创建带扩展表头th
		 */
	    createContrTh:function(){
			var th='';
			var opt=this.options;
			$.each(opt.head,function(i,head){
				if(i==0){
				 th+="<th class="+opt.firstthclass+">"+head+" <a class="+opt.contraclass+" value='1' title="+opt.expandtitle+" href='javascript:void(0)' onclick=showcolom(this);> >>></a>"
				}else{
				 th+="<th>"+head+"</th>";
				}
				th+=head;
			});
			return th;
		},
		call : function(func, event) {   //重写回调事件
			var result = null;
			if ($.isFunction(func))
				result = func.apply(this, [ event || window.event ]
						.concat(Array.prototype.slice.call(arguments, 2)));
			return result;
		},
		addtrmouse:function(){
			$("table tr").mouseover(function(){
		        	$(this).addClass("hover");
		        });
		     $("table tr").mouseout(function(){
		        	$(this).removeClass("hover");
		   });
		},
	   matchEvent:function(event) {
			// 捕获enter code事件
			var code = event&&event!=true?event.keyCode||event.which:-1;
		
			return false;
		},
		/**
		 * 创建副维度
		 * floor
		 */
	  createdvicedim:function(floor,name,value,otherfloor,orgfloor){
			var td="<td>";
			var opt=this.options;
			if(opt.vicedim.length>0){
				$.each(opt.vicedim,function(i,objs){
					var obj=objs;
					td+="<a href='javascript:void(0)' stext='"+name+"' path='"+opt.viceaction+"?conhasqd="+opt.conhasqd+
					"&mainisqd="+opt.maindim.isqd+"&dimisqd="+obj.isqd+"&dimcode="+obj.dimcode+"&downvalue="+value+
					"&dimname="+obj.dimname+"&table="+obj.table+"&dimmaxfloor="+obj.maxfloor+"&isothertable="+opt.isothertable+
					"&otherflag="+otherfloor+"&orgfloor="+orgfloor+"' onclick='parent.uitcon.tabs.add(this);return false'>"+obj.dimname+"</a>";
					if(floor<opt.maindim.maxfloor){
					 td+="|";
					}
				});
			}
			if(floor<opt.maindim.maxfloor){
			     td+="<span downname='exceldown' text='"+name+"' groupcode='"+value+"' ><a href='javascript:void(0)'>下载</a></span>";
			}else{
			     td+="&nbsp;";	 
			}
			td+="</td>";
			return td;
		}
	});
	$.fn.MyTreeTable = function(options) {
		options = options || {};
		return this.each( function() {
			new MyTreeTable(this, options);
		});
	};
	
})(jQuery);